import React from "react";
import BaseComponent from "../../../redare/components/BaseComponent";
import {Input, Modal} from "antd";
import UserPresenter from "../../../.gen/presenter/UserPresenter";

export default class ModifyEmailModal extends BaseComponent {

    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            loading: false
        };
        this.userPresenter = new UserPresenter(this);
    }

    //邮箱改变
    emailInputChange(e) {
        this._form.email = e.target.value;
    }

    //点击编辑确定
    confirmChangeEmail() {
        let {user, email} = this._form;
        if (!email) {
            this.toastFail('请输入邮箱');
            return
        }
        this.setButtonLoading(true);
        let where = {userId: user.userId};
        this.userPresenter.editUser(
            {where, email: email},
            {},
            () => {
                this.setButtonLoading(false);
                this.setModalVisible(false);
                this.sendEvent('edit-email-success-callback');
                this.toastSuccess('编辑成功')
            },
            () => {
                this.setButtonLoading(false)
            })
    }

    render() {
        return (
            <Modal
                title="邮箱修改"
                centered
                okText={"确认修改"}
                cancelText={'取消'}
                maskClosable={false}
                destroyOnClose={true}
                keyboard={true}
                visible={this.state.visible}
                confirmLoading={this.state.loading}
                onOk={() => this.confirmChangeEmail()}
                onCancel={() => this.setModalVisible(false)}
            >
                <Input placeholder="邮箱" defaultValue={this._form.user ? this._form.user.email : null} onChange={this.emailInputChange.bind(this)}/>
            </Modal>
        );
    }

    _show(user) {
        this._form.user = user;
        this.setModalVisible(true)
    }

    setModalVisible(visible) {
        this.setState({visible})
    }

    setButtonLoading(loading) {
        this.setState({loading})
    }

}